import {router} from "@/main.js";
import {getToken} from "@/utils/auth.js";
import {
    getCurrentPid,
    setCurrentPid
} from "@/utils/project_core.js";
import issue from "@/store/modules/issue.js";

const sys = {
    state: {
        navigationCode: '',
        name: 'TaskSaas',
        template: '',
        current_pid : getCurrentPid(),
    },
//更改state的mutation，只能进行同步
    mutations: {
        SET_SYS_NAV_CODE: (state, code) => {
            state.navigationCode = code
        },
        SET_SYS_TEMP: (state, temp) => {
            state.template = temp
        },
        SET_CURRENT_PID:(state,pid)=>{
            state.current_pid = pid
            setCurrentPid(pid)
        }

    },

//包含了mutations的action，可以使用异步
    actions: {
        goDashboardBread(){
            router.push('/dashboard?pid='+sys.state.current_pid)
        },
        goIssueListBread(){
            router.push('/issueList?status='+issue.state.issue_status)
        },
        goIssueDetailBread(){
            router.push('/issueDetail?pk='+issue.state.current_issue_pk)
        },
        projectChange({commit},key){
            console.log('projectChange:',key)
            commit('SET_CURRENT_PID',key)
        },

        navigationChange({commit}, key) {
            console.log('key:', key)
            commit('SET_SYS_NAV_CODE',key)
            console.log('navigationCode:',this.getters.navigationCode)
            if (!key){return}
            if (key == 1) {
                router.push('/')
            } else if (key == 2) {
                if(getToken()){
                    router.push('/projectList')
                }else{
                    layer.msg("请先进行登录")
                    router.push('/')
                }
            }else{
                // layer.msg('error key')
            }
        },
    },

    getters: {
        navigationCode: state => state.navigationCode,
        template: state => state.template,
        current_pid: state => state.current_pid,

    }
}

export default sys
